/**
 * 
 */
package com.shy.base.util;

import static com.alibaba.druid.filter.config.ConfigTools.encrypt;
import static com.alibaba.druid.filter.config.ConfigTools.genKeyPair;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import cn.hutool.core.codec.Base64Encoder;

/**
 * Title: DatabaseEncry
 * Description:
 * @author H2013788
 * @date 2022年9月11日
 */
public class DatabaseEncry {

    public static void main(String[] args) {
        /*try {
            String password = "FWk4SE6m7aG4LXRp";
            String[] arr = genKeyPair(512);
            System.out.println("privateKey:" + arr[0]);
            System.out.println("publicKey:" + arr[1]);
            System.out.println("password:" + encrypt(arr[0], password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        System.out.println(aesEncode("shy123"));
    }
    
    
    
    /**
     * 生成随机数
     * @param length
     * @return
     */
    private static String getChar(int length) {
        char[] ss = new char[length];
        int i=0;
        while(i<length) {
            int f = (int) (Math.random()*3);
            if(f==0)
                ss[i] = (char) ('A'+Math.random()*26);
            else if(f==1)
                ss[i] = (char) ('a'+Math.random()*26);
            else
                ss[i] = (char) ('0'+Math.random()*10);
            i++;
        }
        String str = new String(ss);
        return str;
    }
 
    /**
     * 随机数 + AES加密
     * @param password
     * @return
     */
    public static String aesEncode(String password) {
        String encodePassword = "";
        String key = "abcdef0123456789"; // 16位，可自由定义，长度必须为16
        String iv = "0123456789abcdef"; // 16位，可自由定义，长度必须为16
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding"); // 参数分别代表 算法名称/加密模式/数据填充方式
            int blockSize = cipher.getBlockSize();
 
            byte[] dataBytes = password.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
 
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
 
            String encode = new Base64Encoder().encode(encrypted);
            //如果数据过长base64会自动添加换行符
            encode=encode.replaceAll(System.lineSeparator(), "");
            encodePassword = getChar(9)+encode; //随机数加加密后的密码
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encodePassword;
    }

    
     /**
      * function aesDecode(encodePassword) {
        var d = encodePassword.substr(9);
        var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');//需要与后台保持一致
        var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');//需要与后台保持一致
        var decrypted = CryptoJS.AES.decrypt(d, key, {
            iv : iv,
            padding : CryptoJS.pad.ZeroPadding
        });
        var password = decrypted.toString(CryptoJS.enc.Utf8);
        return password;
        }
        
        function _038fx79e034(encodePassword) {
            var d = encodePassword.substr(9);
            var key = CryptoJS.enc.Latin1.parse(x0E+x69+x38+x47+x02);
            var iv = CryptoJS.enc.Latin1.parse(x38+x47+x02+x0E+x69);
            var decrypted = CryptoJS.AES.decrypt(d, key, {
                iv : iv,
                padding : CryptoJS.pad.ZeroPadding
            });
            var password = decrypted.toString(CryptoJS.enc.Utf8);
            return password;
        }
        
       setInterval("checkPage()", 1000);
       function checkPage(){
           $.ajax({
               url:"commonApi/c?t=" + page,
               type:"POST",
               dataType:"JSON",
               success:function(data){
                   console.info(data.message);
                   if (!data.success){
                       window.location.href = "about:blank";
                   }
               }
           })
       }
     */
    
}
